package jee.boot.common.utils.excel.service;

import jee.boot.common.cache.CacheDataManager;
import jee.boot.common.utils.SpringContextHolder;
import jee.boot.common.utils.StringUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字段类型工具类
 */
public class EntityTranslateHelper {
    private Logger logger= LoggerFactory.getLogger(getClass());
    private Map<String, List<Object>> dataMap = new HashMap<>();

    private EntityTranslateHelper() {
        this.dataMap = new HashMap<>();
    }

    public static EntityTranslateHelper getInstance() {
        return new EntityTranslateHelper();
    }

    /**
     * 将导入的字符串类型的值转换成对应对象,根据excel中的名称等信息查找数据回填对应的id
     *
     * @param fieldType 字段类型Class,比如SysOffice,SysUser等
     * @param val       excel中填写的值,比如 "普华永道"
     * @param attrName  属性名称,比如company.name
     * @return 返回对应的对象 ,比如{"id":"guid1234567890","name":"普华永道"}
     */
    public Object getValue(Class<?> fieldType, String val, String attrName) {
        List<Object> dataList = getDataList(fieldType);
        for (Object o : dataList) {
            if (StringUtils.equals(getFieldValue(o, StringUtils.substringAfter(attrName, ".")), val)) {
                return o;
            }
        }
        return null;
    }
    /**
     * 主数据翻译
     * @param fieldType 字段类型,比如jee.boot.entity.SysOffice等
     * @param val       excel中填写的值,比如 "普华永道"
     * @param attrName  属性名称,比如name
     * @param returnAttrName  返回属性名称,比如code
     * @return 返回对象的returnAttrName属性 ,比如 pwc
     */
    public Object entityFieldValue(String  fieldType, String val, String attrName,String returnAttrName) {
        Class<?> aClass = null;
        try {
            aClass = Class.forName(fieldType);
            List<Object> dataList = getDataList(aClass);
            for (Object o : dataList) {
                if (StringUtils.equals(getFieldValue(o,attrName), val)) {
                    return getFieldValue(o,returnAttrName);
                }
            }
        } catch (ClassNotFoundException e) {
            logger.error(e.getMessage(),e);
        }
        return null;
    }
    /**
     * 按类型读取缓存主数据列表
     *
     * @param fieldType
     * @return
     */
    public List<Object> getDataList(Class<?> fieldType) {
        List<Object> list = dataMap.get(fieldType.getName());
        if (list == null) {
            try {
                Object o = fieldType.newInstance();
                String type = getFieldValue(o, "cacheKey");
                list = getCacheDataManager().getCacheData(type);
                if (list == null) {
                    list = new ArrayList<>();
                }
                dataMap.put(fieldType.getName(), list);
            } catch (Exception e) {
                e.printStackTrace();
                list = new ArrayList<>();
            }
        }
        return list;
    }

    private String getFieldValue(Object object, String field) {
        try {
            return (String) PropertyUtils.getProperty(object, field);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 清理缓存使用
     */
    public void clear() {
        dataMap.clear();
    }

    public CacheDataManager getCacheDataManager() {
        return SpringContextHolder.getBean(CacheDataManager.class);
    }

}
